﻿using System;
using System.Diagnostics;

namespace Zo.Common
{
    /// <summary>
    /// 程序执行性能测试工具。
    /// </summary>
    public class ExecuteUseTime
    {
        /// <summary>
        /// 执行。
        /// </summary>
        /// <param name="num">执行次数。</param>
        /// <param name="action">执行程序。</param>
        /// <returns>消耗时长（单位：毫秒）。</returns>
        public static long Execute(int num, Action action)
        {
            var watch = new Stopwatch();
            watch.Start();

            for (var index = 0; index < num; index++)
            {
                action();
            }

            watch.Stop();

            return watch.ElapsedMilliseconds;
        }
    }
}